﻿namespace FFF.Test.Application.AppServices.PublicService.SignalRs
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Auditing;
    using Abp.Dependency;
    using Abp.Json;
    using Abp.Runtime.Caching;
    using Abp.UI;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.SignalR;
    using FFF.Test.Entity;
    using FFF.Test.Entity.FW.View;
    using FFF.Test.Util.Consts;
    using Ydm.Extensions;

    /// <summary>
    /// SignalR的管理对象
    /// </summary>
    public class SignalRAppService : YdmAbpAppServiceBase, ISignalRAppService
    {
        /// <summary>
        /// 依赖注入容器
        /// </summary>
        public IIocResolver IocResolver { get; set; }

        /// <summary>
        /// 依赖注入容器
        /// </summary>
        public ICacheManager CacheManager { get; set; }

        /// <summary>
        /// http上下文
        /// </summary>
        public IHttpContextAccessor HttpContext { get; set; }

        private readonly IHubContext<MyChatHub> _myChatHub;

        /// <summary>
        /// Initializes a new instance of the <see cref="SignalRAppService"/> class.
        /// SignalR构造方法
        /// </summary>
        /// <param name="myChatHub">hub</param>
        public SignalRAppService(IHubContext<MyChatHub> myChatHub)
        {
            this._myChatHub = myChatHub;
        }

        /// <summary>
        /// 获取用户的SingleR的ConnectionId
        /// </summary>
        /// <param name="userGUID">用户GUID</param>
        /// <param name="conId">连接的Id</param>
        /// <returns>SignalR对象</returns>
        public async Task SetUserConId(Guid userGUID, string conId)
        {
            await this.CacheManager
                     .GetCache(CacheAppConsts.UserStoreCache)
                     .SetAsync(userGUID + "SignalR", conId);
        }

        /// <summary>
        /// 移除用户的SingleR的ConnectionId
        /// </summary>
        /// <param name="userGUID">用户GUID</param>
        /// <returns>SignalR对象</returns>
        public async Task RemoveUserConId(Guid userGUID)
        {
            await this.CacheManager
                     .GetCache(CacheAppConsts.UserStoreCache)
                    .RemoveAsync(userGUID + "SignalR");
        }

        /// <summary>
        /// 获取用户的ConID
        /// </summary>
        /// <param name="userGUID">用户GUID</param>
        /// <returns>ConId</returns>
        public async Task<string> GetUserConId(Guid userGUID)
        {
            var conId = await this.CacheManager
                     .GetCache(CacheAppConsts.UserStoreCache)
                     .GetOrDefaultAsync(userGUID + "SignalR");
            return conId?.ToString();
        }

        /// <summary>
        /// 获取用户的ConID
        /// </summary>
        /// <param name="userGUID">用户GUID</param>
        /// <param name="message">发送的信息</param>
        /// <returns>ConId</returns>
        public async Task<bool> SendMessage(Guid userGUID, string message)
        {
            var conId = await this.GetUserConId(userGUID);
            if (conId != null)
            {
                await this._myChatHub.Clients.Client(conId).SendAsync("getMessage", message);
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
